package com.yixing.tech.entity.entity.attr;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.CustomAggregationOperation;
import com.yixing.tech.entity.entity.RequestType;
import com.yixing.tech.common.vo.s5.EntitySchema;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;

import java.util.List;

/**
 * @author yixing tech
 * at 2021/10/9
 * describe：
 */
public final class UserGroup extends AttrType {
    public UserGroup() {
        super("用户组", "userGroup");
    }

    @Override
    public void out(List<AggregationOperation> operations, EntitySchema.AttrVO attr) throws BusinessException {
        super.out(operations,attr);
        String sql = "{ $lookup:{ " +
                "from:'userGroup'," +
                "let:{ location_id:'$" + attr.getCode() + "'}," +
                "pipeline:[" +
                "{$match:{$expr:{$eq:['$_id',{$ifNull :['$$location_id',[]]}]}}}," +
                "{$project:{name:1}}" +
                "]," +
                "as:'" + attr.getCode() + "'}}";
        operations.add(new CustomAggregationOperation(sql));
        operations.add(Aggregation.unwind(attr.getCode(), true));
    }

    @Override
    public void out(List<AggregationOperation> operations, EntitySchema.AttrVO attr, RequestType requestType) throws BusinessException {
        out(operations, attr);
    }

    /**
     * 数据插入处理
     *
     * @param source
     * @param attr
     * @return
     * @throws BusinessException
     */
    @Override
    public Object in(Object source, EntitySchema.AttrVO attr) throws BusinessException {
        return super.inWithList(source);
    }
}
